package com.backstage.configuration.shiro;

import com.backstage.common.uitil.DES_3;
import com.backstage.entity.User;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authc.credential.SimpleCredentialsMatcher;
import org.apache.shiro.subject.SimplePrincipalCollection;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

/**
 * @Project authority
 * @Package com.backstage.configuration.shiro
 * @Data 2017/7/17下午 12:00.
 * @Aurhor 阮雪峰
 */
public class CustomCredentialsMatcher extends SimpleCredentialsMatcher {

    @Override
    public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
        UsernamePasswordToken t = (UsernamePasswordToken) token;
        String password = new String(t.getPassword());
        String username = t.getUsername();
        SimplePrincipalCollection collection = (SimplePrincipalCollection) info.getPrincipals();
        Collection c = collection.fromRealm("authorityRealm");
        User user = (User) ((Set)c).iterator().next();
        return username.equals(user.getUser_name()) && password.equals(DES_3.decryptBy3DES(user.getPassword(), user.getDes_key()));
    }
}
